package com.mengshu.security;

import com.mengshu.security.filter.ParamsSignRequestFilter;
import com.mengshu.security.handler.IParamsSignHandler;
import com.mengshu.security.handler.Md5ParamsSignHandler;
import com.mengshu.security.handler.ParamsSignFailureHandler;
import com.mengshu.security.properties.SecurityProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Optional;

@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties(SecurityProperties.class)
@ConditionalOnProperty(prefix = SecurityProperties.PREFIX, name = "enable", havingValue = "true", matchIfMissing = true)
public class SecurityAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public ParamsSignRequestFilter paramsSignRequestFilter(SecurityProperties securityProperties, IParamsSignHandler paramsSignHandler) {
        return new ParamsSignRequestFilter(securityProperties, paramsSignHandler);
    }

    @Bean
    @ConditionalOnMissingBean
    public IParamsSignHandler paramsSignHandler(SecurityProperties securityProperties, Optional<ParamsSignFailureHandler> paramsSignFailureHandler) {
        return new Md5ParamsSignHandler(securityProperties, paramsSignFailureHandler.orElse(null));
    }

}
